﻿using KWT.DRG_DIP.API.Infrastructure;
using KWT.DRG_DIP.DO.System;
using KWT.DRG_DIP.DTO.Basic.Module;
using KWT.DRG_DIP.PO.Basic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace KWT.DRG_DIP.API.Controllers.Basic
{
    /// <summary>
    /// 系统模块
    /// </summary>
    [ApiExplorerSettings(GroupName = "basic")]
    public class ModuleController : BasicControllerBase
    {
        /// <summary>
        /// 模块列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public OutputResult All()
        {
            var list = main.Set<Sys_Module>().Select(x => new { x.ModuleID, x.Name }).ToList();
            return Output(list.Count > 0, "", "", list);
        }

        /// <summary>
        /// 添加或修改模块
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(Output<string>), 200)]
        public OutputResult AddOrUpdate(InAddOrUpdate dto)
        {
            if (string.IsNullOrEmpty(dto.ModuleID))
            {
                var module = new Sys_Module()
                {
                    ModuleID = "FC" + snow_id.CreateId().ToString(),
                    Name = dto.Name,
                };
                ef.Add(module);
            }
            else
            {
                var module = ef.Find<Sys_Module>(dto.ModuleID);
                module.Name = dto.Name;
                ef.Attach(module);
            }
            var flag = ef.SaveChanges() > 0;
            return Output(flag, "操作成功！", "操作失败！");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="moduleIDs">模块ID集合</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<string>), 200)]
        public OutputResult Del(string[] moduleIDs)
        {
            if (moduleIDs.Length <= 0)
            {
                return Fail("入参错误");
            }
            var flag = ef.Set<Sys_Module>().Where(x => moduleIDs.Contains(x.ModuleID)).ExecuteDelete() > 0;
            return Output(flag, "操作成功！", "操作失败！");
        }

        /// <summary>
        /// 根据当前用户获得模块列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(Output<List<OutSysModule>>), 200)]
        public OutputResult GetList()
        {
            var result = (from x in ef.Set<Sys_Module>().AsQueryable()
                          join y in ef.Set<Module_Tenant>().AsQueryable()
                          on x.ModuleID equals y.ModuleID
                          where y.TenantID == CurrentUser.TenantID
                          select new OutSysModule()
                          {
                              ModuleID = x.ModuleID,
                              Name = x.Name
                          }).ToList();
            return Output(result != null, "成功", "失败", result);
        }

        /// <summary>
        /// 为租户授权或解除模块
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(Output<string>), 200)]
        public OutputResult SetTanent(InSetTanent dto)
        {
            var flag = ef.Set<Module_Tenant>().Any(x => x.TenantID == dto.TanentID && x.ModuleID == dto.ModuleID);
            if (flag)
            {
                var mt = new Module_Tenant()
                {
                    MTID = "MT" + snow_id.ToString(),
                    ModuleID = dto.ModuleID,
                    TenantID = dto.TanentID
                };
                ef.Add(mt);
                var result = ef.SaveChanges() > 0;
                return Output(result, "授权成功", "授权失败");
            }
            else
            {
                var mt = ef.Set<Module_Tenant>().SingleOrDefault(x => x.ModuleID == dto.ModuleID && x.TenantID == dto.TanentID);
                if (mt != null)
                {
                    ef.Remove(mt);
                    var result = ef.SaveChanges() > 0;
                    return Output(result, "解除成功", "解除失败");
                }
                else
                {
                    return Fail("未找到所属关系！");
                }
            }
        }
    }
}
